

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Statsmodels Exporter &mdash; Nyoka 4.4.0 documentation</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

  
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home" alt="Documentation Home"> Nyoka
          

          
          </a>
<div class="version">
                4.4
              </div>
          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="../statsmodels_to_pmml.html">Statsmodels Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../keras_model_to_pmml.html">Keras Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../retinanet.html">RetinaNet Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../lgb_to_pmml.html">LightGBM Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../pre_process.html">Pre-Processing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../skl_to_pmml.html">Scikit-Learn Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../xgboost_to_pmml.html">XGBoost Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../exponential_smoothing.html">ExponentialSmoothing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../preprocess_nyoka.html">Nyoka's Pre-Processing Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../enums.html">Enums Module</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Nyoka</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">Module code</a> &raquo;</li>
        
          <li>Statsmodels Exporter</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for Statsmodels Exporter</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Copyright (c) 2004-2016 Zementis, Inc.</span>
<span class="sd"> Copyright (c) 2016-2020 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its</span>

<span class="sd"> SPDX-License-Identifier: Apache-2.0</span>

<span class="sd">   Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="sd">   you may not use this file except in compliance with the License.</span>
<span class="sd">   You may obtain a copy of the License at</span>

<span class="sd">       http://www.apache.org/licenses/LICENSE-2.0</span>

<span class="sd">   Unless required by applicable law or agreed to in writing, software</span>
<span class="sd">   distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="sd">   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="sd">   See the License for the specific language governing permissions and</span>
<span class="sd">   limitations under the License.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">absolute_import</span>

<span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span>
<span class="n">BASE_DIR</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">BASE_DIR</span><span class="p">)</span>


<span class="kn">from</span> <span class="nn">pprint</span> <span class="kn">import</span> <span class="n">pprint</span>
<span class="kn">from</span> <span class="nn">PMML44</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">metadata</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">from</span> <span class="nn">enums</span> <span class="kn">import</span> <span class="o">*</span>

<div class="viewcode-block" id="StatsmodelsToPmml"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml">[docs]</a><span class="k">class</span> <span class="nc">StatsmodelsToPmml</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Exports time-series models from statsmodels library into PMML.</span>

<span class="sd">    Parameters:</span>
<span class="sd">    -----------</span>
<span class="sd">    results_obj: </span>
<span class="sd">        Instance of AR(I)MAResultsWrapper / (SARI/VAR)MAXResultsWrapper from statsmodels</span>
<span class="sd">    pmml_file_name: string</span>
<span class="sd">        Name of the PMML</span>
<span class="sd">    conf_int : list (optional)</span>
<span class="sd">        Confidence intervel. A list of values mentioning the percentage of confidence.</span>
<span class="sd">        e.g., conf_int = [80,95] will create OutputField for lower bound and upper bound of confidence interval with 80% and 95%.</span>
<span class="sd">    model_name : string (optional)</span>
<span class="sd">        Name of the model</span>
<span class="sd">    description : string (optional)</span>
<span class="sd">        Description of the model</span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Generates PMML object and exports it to `pmml_file_name`</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">results_obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">pmml_file_name</span><span class="o">=</span><span class="s2">&quot;from_statsmodels.pmml&quot;</span><span class="p">,</span> <span class="n">conf_int</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">model_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span> <span class="o">=</span> <span class="n">results_obj</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pmml_file_name</span> <span class="o">=</span> <span class="n">pmml_file_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conf_int</span> <span class="o">=</span> <span class="n">conf_int</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="o">=</span> <span class="n">model_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="n">description</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pmml</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">construct_pmml</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">export_pmml</span><span class="p">()</span>

<div class="viewcode-block" id="StatsmodelsToPmml.export_pmml"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml.export_pmml">[docs]</a>    <span class="k">def</span> <span class="nf">export_pmml</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Writes the generated PMML object to given `pmml_file_name`</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">pmml</span> <span class="o">=</span> <span class="n">PMML</span><span class="p">(</span>
            <span class="n">version</span><span class="o">=</span><span class="n">PMML_SCHEMA</span><span class="o">.</span><span class="n">VERSION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
            <span class="n">Header</span><span class="o">=</span><span class="n">Header</span><span class="p">(</span>
                <span class="n">copyright</span> <span class="o">=</span> <span class="s2">&quot;Copyright (c) 2018 Software AG&quot;</span><span class="p">,</span>
                <span class="n">description</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span><span class="p">,</span>
                <span class="n">Timestamp</span> <span class="o">=</span> <span class="n">Timestamp</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()),</span>
                <span class="n">Application</span><span class="o">=</span><span class="n">Application</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;Nyoka&quot;</span><span class="p">,</span><span class="n">version</span><span class="o">=</span><span class="n">metadata</span><span class="o">.</span><span class="n">__version__</span><span class="p">)</span>
            <span class="p">),</span>
            <span class="n">DataDictionary</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">data_dictionary</span><span class="p">,</span>
            <span class="n">TimeSeriesModel</span><span class="o">=</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">ts_model</span><span class="p">]</span>
        <span class="p">)</span>
        <span class="n">pmml</span><span class="o">.</span><span class="n">export</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pmml_file_name</span><span class="p">,</span><span class="s1">&#39;w&#39;</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span></div>

<div class="viewcode-block" id="StatsmodelsToPmml.generate_data_dictionary"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml.generate_data_dictionary">[docs]</a>    <span class="k">def</span> <span class="nf">generate_data_dictionary</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates DataDictionary Object. The number of DataField is one more than the dimension of the data.\</span>
<span class="sd">        The extra DataField is a supplementary to hold the value of `h`(horizon) for forecasting.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">data_fields</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">:</span>
            <span class="n">data_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">DataField</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="n">val</span><span class="p">,</span>
                    <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="n">data_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">DataField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;h&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">INTEGER</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data_dictionary</span> <span class="o">=</span> <span class="n">DataDictionary</span><span class="p">(</span>
            <span class="n">numberOfFields</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">data_fields</span><span class="p">),</span>
            <span class="n">DataField</span><span class="o">=</span><span class="n">data_fields</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="StatsmodelsToPmml.construct_pmml"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml.construct_pmml">[docs]</a>    <span class="k">def</span> <span class="nf">construct_pmml</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Constructs the actual model object. (ARIMA/ TimeSeriesModel)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="s1">&#39;int&#39;</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">dtype</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">endog</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;float64&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">endog</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;float64&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">data_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">ynames</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;str&quot;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">generate_data_dictionary</span><span class="p">()</span>

        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_output</span><span class="p">()</span>
        <span class="n">mining_schema</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_mining_schema</span><span class="p">()</span>
        <span class="n">time_series_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_time_series</span><span class="p">()</span>
        <span class="n">arima_model</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">state_space_model</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;ARMA&#39;</span><span class="p">,</span> <span class="s1">&#39;ARIMA&#39;</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="k">else</span> <span class="s2">&quot;ArimaModel&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="k">else</span> <span class="s2">&quot;Non-Seasonal Arima Model&quot;</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="p">,</span><span class="s2">&quot;fit_details&quot;</span><span class="p">):</span>
                <span class="n">best_fit</span> <span class="o">=</span> <span class="n">TIMESERIES_ALGORITHM</span><span class="o">.</span><span class="n">STATE_SPACE_MODEL</span><span class="o">.</span><span class="n">value</span>
                <span class="n">state_space_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_state_space_model</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">best_fit</span> <span class="o">=</span> <span class="n">TIMESERIES_ALGORITHM</span><span class="o">.</span><span class="n">ARIMA</span><span class="o">.</span><span class="n">value</span>
                <span class="n">arima_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_arima_model</span><span class="p">()</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;VARMAX&#39;</span><span class="p">,</span><span class="s1">&#39;SARIMAX&#39;</span><span class="p">]:</span>
            <span class="n">best_fit</span> <span class="o">=</span> <span class="n">TIMESERIES_ALGORITHM</span><span class="o">.</span><span class="n">STATE_SPACE_MODEL</span><span class="o">.</span><span class="n">value</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="k">else</span> <span class="s2">&quot;State Space Model&quot;</span>
            <span class="n">state_space_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_state_space_model</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;Not Implemented. Currently we support only ARMA, ARIMA, SARIMAX and VARMAX.&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ts_model</span> <span class="o">=</span> <span class="n">TimeSeriesModel</span><span class="p">(</span>
            <span class="n">modelName</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">,</span>
            <span class="n">functionName</span><span class="o">=</span><span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">TIMESERIES</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
            <span class="n">bestFit</span><span class="o">=</span><span class="n">best_fit</span><span class="p">,</span>
            <span class="n">MiningSchema</span><span class="o">=</span><span class="n">mining_schema</span><span class="p">,</span>
            <span class="n">Output</span><span class="o">=</span><span class="n">output</span><span class="p">,</span>
            <span class="n">TimeSeries</span><span class="o">=</span><span class="n">time_series_list</span><span class="p">,</span>
            <span class="n">ARIMA</span><span class="o">=</span><span class="n">arima_model</span><span class="p">,</span>
            <span class="n">StateSpaceModel</span><span class="o">=</span><span class="n">state_space_model</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="StatsmodelsToPmml.generate_state_space_model"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml.generate_state_space_model">[docs]</a>    <span class="k">def</span> <span class="nf">generate_state_space_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Constructs StateSpaceModel object. For the following models -\</span>
<span class="sd">        - `statsmodels.tsa.statespace.sarimax.SARIMAX`</span>
<span class="sd">        - `statsmodels.tsa.statespace.varmax.VARMAX`</span>
<span class="sd">        - `statsmodels.tsa.statespace.tsa.arima.ARIMA`</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
        <span class="n">np</span><span class="o">.</span><span class="n">set_printoptions</span><span class="p">(</span><span class="n">precision</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
        <span class="n">selected_state_cov_matrix</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">predicted_state_cov_matrix</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">observation_cov_matrix</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">smoother_results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">smoother_results</span>
        <span class="n">S_t0</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">filtered_state</span><span class="p">[</span><span class="o">...</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">F_matrix</span> <span class="o">=</span> <span class="n">smoother_results</span><span class="o">.</span><span class="n">transition</span><span class="p">[</span><span class="o">...</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1">#transition_matrix</span>
        <span class="n">G</span> <span class="o">=</span> <span class="n">smoother_results</span><span class="o">.</span><span class="n">design</span><span class="p">[</span><span class="o">...</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1">#measurement_matrix</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;ARMA&quot;</span><span class="p">,</span><span class="s2">&quot;ARIMA&quot;</span><span class="p">]:</span>
            <span class="n">intercept</span> <span class="o">=</span> <span class="p">[</span><span class="n">smoother_results</span><span class="o">.</span><span class="n">obs_intercept</span><span class="p">[</span><span class="o">...</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span>
            <span class="n">intercept_type</span> <span class="o">=</span> <span class="s2">&quot;observation&quot;</span>
            <span class="n">S_t1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">F_matrix</span><span class="p">,</span> <span class="n">S_t0</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1">#state_intercept might contain `nan` values. So here it is generated manually.</span>
            <span class="n">intercept</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">S_t0</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">k_trend</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;VARMAX&#39;</span><span class="p">:</span>
                    <span class="n">mu</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_params_trend</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">mu</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;Series&#39;</span><span class="p">:</span>
                        <span class="n">mu</span> <span class="o">=</span> <span class="n">mu</span><span class="o">.</span><span class="n">values</span>
                    <span class="n">intercept</span><span class="p">[:</span><span class="nb">len</span><span class="p">(</span><span class="n">mu</span><span class="p">)]</span> <span class="o">+=</span> <span class="n">mu</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">mu</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">_params_trend</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">mu</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;Series&#39;</span><span class="p">:</span>
                        <span class="n">mu</span> <span class="o">=</span> <span class="n">mu</span><span class="o">.</span><span class="n">values</span>
                    <span class="n">spec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">specification</span>
                    <span class="n">k_state</span> <span class="o">=</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;k_diff&#39;</span><span class="p">]</span><span class="o">+</span><span class="n">spec</span><span class="p">[</span><span class="s1">&#39;seasonal_periods&#39;</span><span class="p">]</span><span class="o">*</span><span class="n">spec</span><span class="p">[</span><span class="s1">&#39;k_seasonal_diff&#39;</span><span class="p">]</span>
                    <span class="n">intercept</span><span class="p">[</span><span class="n">k_state</span><span class="p">]</span> <span class="o">+=</span> <span class="n">mu</span>
            <span class="n">intercept_type</span> <span class="o">=</span> <span class="s2">&quot;state&quot;</span>
            <span class="n">S_t1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">F_matrix</span><span class="p">,</span> <span class="n">S_t0</span><span class="p">)</span> <span class="o">+</span> <span class="n">intercept</span>
        <span class="n">arr_content</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">intercept</span><span class="p">])</span>
        <span class="n">arr</span> <span class="o">=</span> <span class="n">ArrayType</span><span class="p">(</span><span class="n">type_</span><span class="o">=</span><span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span><span class="n">content</span><span class="o">=</span><span class="n">arr_content</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">intercept</span><span class="p">))</span>
        <span class="n">intercept_vector</span> <span class="o">=</span> <span class="n">InterceptVector</span><span class="p">(</span><span class="n">Array</span><span class="o">=</span><span class="n">arr</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="n">intercept_type</span><span class="p">)</span>

        <span class="n">t_mat</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">(</span><span class="n">nbRows</span><span class="o">=</span><span class="n">F_matrix</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">nbCols</span><span class="o">=</span><span class="n">F_matrix</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">F_matrix</span><span class="p">:</span>
            <span class="n">array_content</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">row</span><span class="p">])</span>
            <span class="n">t_mat</span><span class="o">.</span><span class="n">add_Array</span><span class="p">(</span><span class="n">ArrayType</span><span class="p">(</span><span class="n">content</span><span class="o">=</span><span class="n">array_content</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
        <span class="n">transition_matrix</span> <span class="o">=</span> <span class="n">TransitionMatrix</span><span class="p">(</span><span class="n">Matrix</span><span class="o">=</span><span class="n">t_mat</span><span class="p">)</span>

        <span class="n">m_mat</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">(</span><span class="n">nbRows</span><span class="o">=</span><span class="n">G</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">nbCols</span><span class="o">=</span><span class="n">G</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">G</span><span class="p">:</span>
            <span class="n">array_content</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">row</span><span class="p">])</span>
            <span class="n">m_mat</span><span class="o">.</span><span class="n">add_Array</span><span class="p">(</span><span class="n">ArrayType</span><span class="p">(</span><span class="n">content</span><span class="o">=</span><span class="n">array_content</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
        <span class="n">measurement_matrix</span> <span class="o">=</span> <span class="n">MeasurementMatrix</span><span class="p">(</span><span class="n">Matrix</span><span class="o">=</span><span class="n">m_mat</span><span class="p">)</span>

        <span class="n">arr_content</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">S_t1</span><span class="p">])</span>
        <span class="n">arr</span> <span class="o">=</span> <span class="n">ArrayType</span><span class="p">(</span><span class="n">type_</span><span class="o">=</span><span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span><span class="n">content</span><span class="o">=</span><span class="n">arr_content</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">S_t1</span><span class="p">))</span>
        <span class="n">final_state_vector</span> <span class="o">=</span> <span class="n">FinalStateVector</span><span class="p">(</span><span class="n">Array</span><span class="o">=</span><span class="n">arr</span><span class="p">)</span>


        <span class="c1">#For confidence interval</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">conf_int</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">R</span> <span class="o">=</span> <span class="n">smoother_results</span><span class="o">.</span><span class="n">selection</span><span class="p">[</span><span class="o">...</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># selection matrix</span>
            <span class="n">Q</span> <span class="o">=</span> <span class="n">smoother_results</span><span class="o">.</span><span class="n">state_cov</span><span class="p">[</span><span class="o">...</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># state_covariance matrix</span>
            <span class="n">obs_cov</span> <span class="o">=</span> <span class="n">smoother_results</span><span class="o">.</span><span class="n">obs_cov</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># observation covariance</span>
            <span class="n">R_Q_R_prime</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">R</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">Q</span><span class="p">,</span><span class="n">R</span><span class="o">.</span><span class="n">T</span><span class="p">))</span> <span class="c1"># selected_state_cov</span>
            <span class="n">P_t0</span> <span class="o">=</span> <span class="n">smoother_results</span><span class="o">.</span><span class="n">predicted_state_cov</span><span class="p">[</span><span class="o">...</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># predicted_state_cov</span>

            <span class="n">RQR_mat</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">(</span><span class="n">nbRows</span><span class="o">=</span><span class="n">R_Q_R_prime</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">nbCols</span><span class="o">=</span><span class="n">R_Q_R_prime</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">R_Q_R_prime</span><span class="p">:</span>
                <span class="n">array_content</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">row</span><span class="p">])</span>
                <span class="n">RQR_mat</span><span class="o">.</span><span class="n">add_Array</span><span class="p">(</span><span class="n">ArrayType</span><span class="p">(</span><span class="n">content</span><span class="o">=</span><span class="n">array_content</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
            <span class="n">selected_state_cov_matrix</span> <span class="o">=</span> <span class="n">SelectedStateCovarianceMatrix</span><span class="p">(</span><span class="n">Matrix</span><span class="o">=</span><span class="n">RQR_mat</span><span class="p">)</span>
            <span class="n">p_mat</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">(</span><span class="n">nbRows</span><span class="o">=</span><span class="n">P_t0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">nbCols</span><span class="o">=</span><span class="n">P_t0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">P_t0</span><span class="p">:</span>
                <span class="n">array_content</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">row</span><span class="p">])</span>
                <span class="n">p_mat</span><span class="o">.</span><span class="n">add_Array</span><span class="p">(</span><span class="n">ArrayType</span><span class="p">(</span><span class="n">content</span><span class="o">=</span><span class="n">array_content</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
            <span class="n">predicted_state_cov_matrix</span> <span class="o">=</span> <span class="n">PredictedStateCovarianceMatrix</span><span class="p">(</span><span class="n">Matrix</span><span class="o">=</span><span class="n">p_mat</span><span class="p">)</span>
            <span class="n">h_mat</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">(</span><span class="n">nbRows</span><span class="o">=</span><span class="n">obs_cov</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">nbCols</span><span class="o">=</span><span class="n">obs_cov</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">obs_cov</span><span class="p">:</span>
                <span class="n">array_content</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">row</span><span class="p">])</span>
                <span class="n">h_mat</span><span class="o">.</span><span class="n">add_Array</span><span class="p">(</span><span class="n">ArrayType</span><span class="p">(</span><span class="n">content</span><span class="o">=</span><span class="n">array_content</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
            <span class="n">observation_cov_matrix</span> <span class="o">=</span> <span class="n">ObservationVarianceMatrix</span><span class="p">(</span><span class="n">Matrix</span><span class="o">=</span><span class="n">h_mat</span><span class="p">)</span>

        <span class="n">state_space_model</span> <span class="o">=</span> <span class="n">StateSpaceModel</span><span class="p">(</span>
            <span class="n">StateVector</span><span class="o">=</span><span class="n">final_state_vector</span><span class="p">,</span>
            <span class="n">TransitionMatrix</span><span class="o">=</span><span class="n">transition_matrix</span><span class="p">,</span>
            <span class="n">MeasurementMatrix</span><span class="o">=</span><span class="n">measurement_matrix</span><span class="p">,</span>
            <span class="n">InterceptVector</span><span class="o">=</span><span class="n">intercept_vector</span><span class="p">,</span>
            <span class="n">SelectedStateCovarianceMatrix</span><span class="o">=</span><span class="n">selected_state_cov_matrix</span><span class="p">,</span>
            <span class="n">PredictedStateCovarianceMatrix</span><span class="o">=</span><span class="n">predicted_state_cov_matrix</span><span class="p">,</span>
            <span class="n">ObservationVarianceMatrix</span><span class="o">=</span><span class="n">observation_cov_matrix</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">state_space_model</span></div>


<div class="viewcode-block" id="StatsmodelsToPmml.generate_arima_model"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml.generate_arima_model">[docs]</a>    <span class="k">def</span> <span class="nf">generate_arima_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Constructs ARIMA object. Only for `statsmodels.tsa.arima_model.ARIMA` class.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">k_ar</span>
        <span class="n">q</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">k_ma</span>
        <span class="n">d</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="p">,</span><span class="s1">&#39;k_diff&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>

        <span class="n">ar</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">ma</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">p</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">ar_content</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">arparams</span><span class="p">])</span>
            <span class="n">ar_params_array</span> <span class="o">=</span> <span class="n">ArrayType</span><span class="p">(</span><span class="n">content</span> <span class="o">=</span> <span class="n">ar_content</span><span class="p">,</span> <span class="n">n</span> <span class="o">=</span> <span class="n">p</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
            <span class="n">ar</span> <span class="o">=</span> <span class="n">AR</span><span class="p">(</span><span class="n">Array</span> <span class="o">=</span> <span class="n">ar_params_array</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">q</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">ma_content</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">coeff</span><span class="p">)</span> <span class="k">for</span> <span class="n">coeff</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">maparams</span><span class="p">])</span>
            <span class="n">ma_coeff_array</span> <span class="o">=</span> <span class="n">ArrayType</span><span class="p">(</span><span class="n">content</span> <span class="o">=</span> <span class="n">ma_content</span><span class="p">,</span> <span class="n">n</span> <span class="o">=</span> <span class="n">q</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
            <span class="n">ny_maCoef_obj</span> <span class="o">=</span> <span class="n">MACoefficients</span><span class="p">(</span><span class="n">Array</span> <span class="o">=</span> <span class="n">ma_coeff_array</span><span class="p">)</span>

            <span class="n">residuals</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">resid</span><span class="p">[</span><span class="o">-</span><span class="n">q</span><span class="p">:]</span>
            <span class="n">resid_content</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">res</span><span class="p">)</span> <span class="k">for</span> <span class="n">res</span> <span class="ow">in</span> <span class="n">residuals</span><span class="p">])</span>
            <span class="n">resid_array</span> <span class="o">=</span> <span class="n">ArrayType</span><span class="p">(</span><span class="n">content</span> <span class="o">=</span> <span class="n">resid_content</span><span class="p">,</span> <span class="n">n</span> <span class="o">=</span> <span class="n">q</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
            <span class="n">residual_obj</span> <span class="o">=</span> <span class="n">Residuals</span><span class="p">(</span><span class="n">Array</span> <span class="o">=</span> <span class="n">resid_array</span><span class="p">)</span>
            <span class="n">ma</span> <span class="o">=</span> <span class="n">MA</span><span class="p">(</span><span class="n">MACoefficients</span> <span class="o">=</span> <span class="n">ny_maCoef_obj</span><span class="p">,</span> <span class="n">Residuals</span> <span class="o">=</span> <span class="n">residual_obj</span><span class="p">)</span>

        <span class="n">const_term</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">k_trend</span><span class="p">:</span>
            <span class="n">const_term</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">results_obj</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">non_seasonal_comp</span> <span class="o">=</span> <span class="n">NonseasonalComponent</span><span class="p">(</span><span class="n">p</span> <span class="o">=</span> <span class="n">p</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">d</span><span class="p">,</span> <span class="n">q</span> <span class="o">=</span> <span class="n">q</span><span class="p">,</span> <span class="n">AR</span> <span class="o">=</span> <span class="n">ar</span><span class="p">,</span> <span class="n">MA</span> <span class="o">=</span> <span class="n">ma</span><span class="p">)</span>

        <span class="n">rmse</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">sigma2</span><span class="p">)</span>

        <span class="n">arima_obj</span> <span class="o">=</span> <span class="n">ARIMA</span><span class="p">(</span><span class="n">constantTerm</span> <span class="o">=</span> <span class="n">const_term</span><span class="p">,</span>
                                <span class="n">predictionMethod</span> <span class="o">=</span> <span class="n">ARIMA_PREDICTION_METHOD</span><span class="o">.</span><span class="n">CSS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                <span class="n">RMSE</span><span class="o">=</span><span class="n">rmse</span><span class="p">,</span>
                                <span class="n">NonseasonalComponent</span> <span class="o">=</span> <span class="n">non_seasonal_comp</span>
                                <span class="p">)</span>
        <span class="k">return</span> <span class="n">arima_obj</span></div>


<div class="viewcode-block" id="StatsmodelsToPmml.generate_time_value_object"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml.generate_time_value_object">[docs]</a>    <span class="k">def</span> <span class="nf">generate_time_value_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates TimeValue object. If data has any index, then the index will be in TimeStamp object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">time_values</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_obj</span><span class="o">.</span><span class="n">dates</span>
        <span class="k">for</span> <span class="n">data_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)):</span>
            <span class="n">tv</span> <span class="o">=</span> <span class="n">TimeValue</span><span class="p">(</span><span class="n">index</span><span class="o">=</span><span class="n">data_idx</span><span class="p">,</span><span class="n">value</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="n">data_idx</span><span class="p">],</span>\
                <span class="n">Timestamp</span><span class="o">=</span><span class="n">Timestamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">indices</span><span class="p">[</span><span class="n">data_idx</span><span class="p">]))</span> <span class="k">if</span> <span class="n">indices</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="kc">None</span><span class="p">)</span>
            <span class="n">time_values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tv</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">time_values</span></div>

<div class="viewcode-block" id="StatsmodelsToPmml.generate_time_series"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml.generate_time_series">[docs]</a>    <span class="k">def</span> <span class="nf">generate_time_series</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates TimeSeries object. The number of TimeSeries object is equal to the dimeansion of the data.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">time_series_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_obj</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">ts</span> <span class="o">=</span> <span class="n">TimeSeries</span><span class="p">(</span><span class="n">usage</span> <span class="o">=</span> <span class="n">TIMESERIES_USAGE</span><span class="o">.</span><span class="n">ORIGINAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">field</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">startTime</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>\
                 <span class="n">endTime</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data_obj</span><span class="o">.</span><span class="n">endog</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span>\
                 <span class="n">TimeValue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_time_value_object</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data_obj</span><span class="o">.</span><span class="n">endog</span><span class="p">))</span>
            <span class="n">time_series_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ts</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data_obj</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]):</span>
                <span class="n">ts</span> <span class="o">=</span> <span class="n">TimeSeries</span><span class="p">(</span><span class="n">usage</span> <span class="o">=</span> <span class="n">TIMESERIES_USAGE</span><span class="o">.</span><span class="n">ORIGINAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">field</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">startTime</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>\
                     <span class="n">endTime</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data_obj</span><span class="o">.</span><span class="n">endog</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span>\
                 <span class="n">TimeValue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_time_value_object</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data_obj</span><span class="o">.</span><span class="n">endog</span><span class="p">[</span><span class="o">...</span><span class="p">,</span><span class="n">i</span><span class="p">]))</span>
                <span class="n">time_series_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ts</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">time_series_list</span></div>


<div class="viewcode-block" id="StatsmodelsToPmml.generate_output"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml.generate_output">[docs]</a>    <span class="k">def</span> <span class="nf">generate_output</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates Output object. If user provides value in `conf_int` parameter, then there will be two OuputField\</span>
<span class="sd">        for each value. One with `feature=confidenceIntervalLower` and another with `feature=confidenceIntervalUpper`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">out_flds</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">y_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">:</span>
            <span class="n">out_flds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">OutputField</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="s2">&quot;predicted_&quot;</span><span class="o">+</span><span class="n">y_</span><span class="p">,</span>
                    <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">STRING</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PREDICTED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">Extension</span><span class="o">=</span><span class="p">[</span><span class="n">Extension</span><span class="p">(</span><span class="n">extender</span><span class="o">=</span><span class="s2">&quot;ADAPA&quot;</span><span class="p">,</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;dataType&quot;</span><span class="p">,</span><span class="n">value</span><span class="o">=</span><span class="s2">&quot;json&quot;</span><span class="p">)]</span>
                    <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">conf_int</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">lower</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">upper</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">percent</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">conf_int</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">y_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">:</span>
                    <span class="n">lower</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">OutputField</span><span class="p">(</span>
                            <span class="n">name</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;conf_int_</span><span class="si">{</span><span class="n">percent</span><span class="si">}</span><span class="s2">_lower_</span><span class="si">{</span><span class="n">y_</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
                            <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">STRING</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">targetField</span><span class="o">=</span><span class="n">y_</span><span class="p">,</span>
                            <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">CONFIDENCE_INTERVAL_LOWER</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">value</span><span class="o">=</span><span class="n">percent</span><span class="p">,</span>
                            <span class="n">Extension</span><span class="o">=</span><span class="p">[</span><span class="n">Extension</span><span class="p">(</span><span class="n">extender</span><span class="o">=</span><span class="s2">&quot;ADAPA&quot;</span><span class="p">,</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;dataType&quot;</span><span class="p">,</span><span class="n">value</span><span class="o">=</span><span class="s2">&quot;json&quot;</span><span class="p">)]</span>
                            <span class="p">)</span>
                    <span class="p">)</span>
                    <span class="n">upper</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">OutputField</span><span class="p">(</span>
                            <span class="n">name</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;conf_int_</span><span class="si">{</span><span class="n">percent</span><span class="si">}</span><span class="s2">_upper_</span><span class="si">{</span><span class="n">y_</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
                            <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">STRING</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">targetField</span><span class="o">=</span><span class="n">y_</span><span class="p">,</span>
                            <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">CONFIDENCE_INTERVAL_UPPER</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">value</span><span class="o">=</span><span class="n">percent</span><span class="p">,</span>
                            <span class="n">Extension</span><span class="o">=</span><span class="p">[</span><span class="n">Extension</span><span class="p">(</span><span class="n">extender</span><span class="o">=</span><span class="s2">&quot;ADAPA&quot;</span><span class="p">,</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;dataType&quot;</span><span class="p">,</span><span class="n">value</span><span class="o">=</span><span class="s2">&quot;json&quot;</span><span class="p">)]</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
            <span class="n">out_flds</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">lower</span> <span class="o">+</span> <span class="n">upper</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">Output</span><span class="p">(</span><span class="n">OutputField</span><span class="o">=</span><span class="n">out_flds</span><span class="p">)</span></div>

<div class="viewcode-block" id="StatsmodelsToPmml.generate_mining_schema"><a class="viewcode-back" href="../statsmodels_to_pmml.html#statsmodels_to_pmml.StatsmodelsToPmml.generate_mining_schema">[docs]</a>    <span class="k">def</span> <span class="nf">generate_mining_schema</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates MiningSchema object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">mining_fields</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">y_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">:</span>
            <span class="n">mining_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="n">y_</span><span class="p">,</span> <span class="n">usageType</span> <span class="o">=</span> <span class="n">FIELD_USAGE_TYPE</span><span class="o">.</span><span class="n">TARGET</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
        <span class="n">mining_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;h&#39;</span><span class="p">,</span> <span class="n">usageType</span> <span class="o">=</span> <span class="n">FIELD_USAGE_TYPE</span><span class="o">.</span><span class="n">SUPPLEMENTARY</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">MiningSchema</span><span class="p">(</span><span class="n">MiningField</span><span class="o">=</span><span class="n">mining_fields</span><span class="p">)</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; Copyright 2020, maintainer@nyoka.org

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>